package cn.wolfcode.trip.base.service.impl;


import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.mapper.StrategyCatalogMapper;
import cn.wolfcode.trip.base.mapper.StrategyContentMapper;
import cn.wolfcode.trip.base.mapper.StrategyDetailMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private StrategyCatalogMapper strategyCatalogMapper;
    @Autowired
    private StrategyContentMapper strategyContentMapper;


    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail) {
        //自动增加序列号

        Date date = new Date();
        //发布
        if (strategyDetail.getState()==StrategyDetail.STATE_RELEASE){
            strategyDetail.setReleaseTime(date);
        }


        StrategyContent content = strategyDetail.getStrategyContent();
        if (strategyDetail.getId()==null) {
            strategyDetail.setCreateTime(date);

            strategyDetailMapper.insert(strategyDetail);

            content.setId(strategyDetail.getId());
            strategyContentMapper.insert(content);
        }else {
            strategyDetailMapper.updateByPrimaryKey(strategyDetail);

            content.setId(strategyDetail.getId());
            strategyContentMapper.insert(content);
        }
    }

    @Override
    public PageInfo<StrategyDetail> queryForList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
        List<StrategyDetail> list = strategyDetailMapper.queryForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public List<StrategyCatalog> listCatalogBystrategyId(Long strategyId) {
        return strategyCatalogMapper.selectCatalogBystrategyId(strategyId);
    }

    @Override
    public StrategyContent getContent(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }
}
